<?php

namespace app\home\controller;
use think\Db;
class Login extends Common {
    public  function goZhuce(){
        return $this->fetch('login/register');
    }
    public  function forget(){
        return $this->fetch('login/forget');
    }
    public  function xieyi(){
        return $this->fetch('login/xieyi');
    }
    public  function gologin(){
        return $this->fetch('login/login');
    }
    /**
     * 账号密码登录
     * @return bool
     * 参数：账号、密码
     * 返回值：0参数错误，1手机号码错误，2密码格式错误，3没有此用户，4密码错误，5成功
     */
    public  function userLogin(){
        if(!isset($_POST['phone']) || !isset($_POST['pass'])){
            return json(['status'=>0,'msg'=>'参数错误']);
        }
        $phone = $_POST['phone'];
        $pass = $_POST['pass'];
        if(preg_match("/^1[3456789]\d{9}$/", $phone)){
            if(empty($pass) || !isset($pass)){
                return json(['status'=>2,'msg'=>'请填写正确的密码']);
            }else{
                $username = Db::table('up_u_user')->where('user_phone',$phone)->find();
                if(empty($username)){
                    return json(['status'=>3,'msg'=>'该手机号未注册，请先进行注册']);
                }else{
                    if($username['user_password'] != md5($pass)){
                        return json(['status'=>4,'msg'=>'密码错误']);
                    }else{
                        if($username['user_status'] == 2){
                            return json(['status'=>100,'msg'=>'账号冻结']);
                        }else{
                            //------------------------
                            if($username['user_openid']){
                                return json(['status'=>101,'msg'=>'此手机号已被绑定']);
                            }
                            //-------------------------
                            $openid = $this->getOpenid();
                            Db::table('up_u_user')->where('user_phone',$phone)
                                ->update(['user_openid'=>$openid]);
                            return json(['status'=>200]);
                        }
                    }
                }
            }
        }else{
            return json(['status'=>1,'msg'=>'请填写正确的手机号码']);
        }
    }

    /**
     * 手机登录或注册时发送的验证码
     * @return bool
     * 参数：phone type(1是登录 2是注册 3是忘记密码，第三方登录和快捷登录传4)
     * 返回值：0参数错误，1手机格式错误，21登录无此号，22注册有此号，3验证码发送失败，4返回md5加密验证码
     */
    public  function getPhoneCode(){//echo 1;die;
        header("Access-Control-Allow-Origin:*");
        if(!isset($_POST['phone']) || !isset($_POST['type'])){
            return json(['status'=>0,'msg'=>'参数错误']);
        }
        $num = rand(100000,999999);
        $phone = $_POST['phone'];
        $type = $_POST['type'];
        if(!preg_match("/^1[3456789]\d{9}$/", $phone)){
            return json(['status'=>1,'msg'=>'请填写正确的手机号码']);
        }
        $username = Db::table('up_u_user')->where('user_phone',$phone)->find();
        if($type != 4){
            if(empty($username) && $type==1){
                return json(['status'=>21,'msg'=>'没有此手机号码用户']);
            }
            if(empty($username) && $type==3){
                return json(['status'=>21,'msg'=>'没有此手机号码用户']);
            }
            if(!empty($username) && $type==2){
                return json(['status'=>22,'msg'=>'已经有此手机号码用户']);
            }
            if(isset($_POST['reset']) && $_POST['reset']==1 && $type==3){
                $openid = $this->getOpenid();
                $duibi = Db::table('up_u_user')
                    ->where('user_openid', $openid)
                    ->value('user_phone');
                if($duibi != $phone){
                    return json(['status'=>45,'msg'=>'此号码不是绑定的号码！']);
                }
            }
        }
        $sendres = $this->duaxin($phone,$num);
        if($sendres['code'] != '00000'){
            return json(['status'=>3,'msg'=>'短信平台错误：'.$sendres['msg']]);
        }else {
            //=============================
            $isset = Db::table('up_phonecode')->where('phone', $phone)->find();
            if ($isset) {
                $res = Db::table('up_phonecode')->where('phone', $phone)->update(['code' => md5($num), 'time2' => time(), 'status' => 0]);
            } else {
                $res = Db::table('up_phonecode')->insert(['phone' => $phone, 'code' => md5($num), 'time2' => time()]);
            }
            if (!$res) {
                return json(['status' => 31, 'msg' => '数据库操作失败']);
            }
            //===============================================
            return json(['status' => 200, 'msg' => md5($num)]);
        }
    }
    /**
     * 手机号码登录
     * @return bool
     * 参数：phone，code
     * 返回：0参数错误，1手机格式错误，21验证码错误，22验证码超时，3无此用户，4成功
     */

    public  function phoneLogin(){
        header("Access-Control-Allow-Origin:*");
        if(!isset($_POST['phone']) || !isset($_POST['code'])){
            return json(['status'=>0,'msg'=>'参数错误']);
        }
        $phone = $_POST['phone'];
        $code = $_POST['code'];

        if(!preg_match("/^1[34578]\d{9}$/", $phone)){
            return json(['status'=>1,'msg'=>'请填写正确的手机号码']);
        }
        //=============================================
        $res = Db::table('up_phonecode')->where('phone',$phone)->find();
        //=============================================================
        if($code != $res['code']){
            return json(['status'=>21,'msg'=>'验证码错误']);
        }
        $now = time();
        if($now - $res['time2'] >= 301){
            return json(['status'=>22,'msg'=>'验证码超时']);
        }
        if($res['status']==1){
            return json(['status'=>23,'msg'=>'验证码已使用']);
        }
        $username = Db::table('up_u_user')
            ->where('user_phone',$phone)->find();
        if(empty($username)){
            return json(['status'=>3,'msg'=>'没有此手机号码用户']);
        }
        if($username['user_status'] == 2){
            return json(['status'=>100,'msg'=>'账号冻结']);
        }else{
            //更新token
            $token = md5(date('YmdHis') . rand(100000,999999));
            Db::table('up_u_user')->where('user_phone',$phone)
                ->update(['user_token'=>$token]);
//                        $rytoken = $this->rongyun($username['user_id'],$username['user_username'],$username['user_image']);
            $username = Db::table('up_u_user')->where('user_phone',$phone)->find();
            Db::table('up_phonecode')->where('phone',$phone)->update(['status'=>1]);
//                        $username['rytoken'] = $rytoken['token'];
            return json(['status'=>200,'msg'=>$username]);
        }
//        return json(['status'=>200,'msg'=>$username]);
    }

    /**
     * 注册
     * @return bool
     * 参数：phone,code,password
     * 返回：0参数错误，1手机格式错误，21验证码错误，22验证码超时，3无此用户，4有其他未填项，5插入登录表失败，200成功
     */

    public  function userReg(){
        header("Access-Control-Allow-Origin:*");
        $phone = $_POST['phone'];
        $code = $_POST['code'];
        $pass = $_POST['password'];
        if(!preg_match("/^1[3456789]\d{9}$/", $phone)){
            return json(['status'=>1,'msg'=>'请填写正确的手机号码']);
        }
        $username = Db::table('up_u_user')->where('user_phone',$phone)->find();
        if(!empty($username)){
            return json(['status'=>3,'msg'=>'已经有此手机号码用户']);
        }
        if($pass=='' || $code==''){
            return json(['status'=>4,'msg'=>'有未填项，请检查']);
        }
        //=============================================
        $res = Db::table('up_phonecode')->where('phone',$phone)->find();
        //=============================================================
        if($code != $res['code']){
            return json(['status'=>21,'msg'=>'验证码错误']);
        }
        $now = time();
        if($now - $res['time2'] >= 301){
            return json(['status'=>22,'msg'=>'验证码超时']);
        }
        if($res['status']==1){
            return json(['status'=>23,'msg'=>'验证码已使用']);
        }
        $openid = $this->getOpenid();
        $token = md5(date('YmdHis').rand(100000,999999));
        $data = array();
        $data['user_password'] = md5($pass);
        $data['user_time'] = date('Y-m-d H:i:s');
        $data['user_token'] = $token;
        $data['user_phone'] = $phone;
        $data['user_username'] = '红集'.rand(1000000,9999999);
        $data['user_openid'] = $openid;
        $id = Db::name('u_user')->insertGetId($data);
        if($id){
            $user = Db::table('up_u_user')
                ->where('user_id',$id)
                ->find();
            Db::table('up_phonecode')->where('phone',$phone)->update(['status'=>1]);
//            //注册完成积分+10
//            Db::table('up_integral')
//                ->insert(['integral_status'=>1,'integral_user_id'=>$id,'integral_time'=>date('Y-m-d H:i:s'),'integral_num'=>10]);
//            Db::table('up_u_user')
//                ->where('user_id',$id)
//                ->setInc('user_integral',10);
//            $rytoken = $this->rongyun($user['user_id'],$user['user_username'],$user['user_image']);
//            $user['rytoken'] = $rytoken['token'];
            return json(['status'=>200,'msg'=>$user]);
        }else{
            return json(['status'=>5,'msg'=>'生成登录数据失败']);
        }
    }



    /**
     * 忘记密码
     * @return bool
     * 参数：phone,code，newpass
     * 返回：0参数错误，1手机格式错误，21验证码错误，22验证码超时，3无此用户，200成功
     */

    public  function forgetPass(){
        header("Access-Control-Allow-Origin:*");
        if(!isset($_POST['phone']) || !isset($_POST['code'])){
            return json(['status'=>0,'msg'=>'参数错误']);
        }
        $phone = $_POST['phone'];
        $code = $_POST['code'];
        $new = $_POST['newpass'];
        if(!preg_match("/^1[3456789]\d{9}$/", $phone)){
            return json(['status'=>1,'msg'=>'请填写正确的手机号码']);
        }
        if($new == '' || $code == ''){
            return json(['status'=>2,'msg'=>'有未填项']);
        }
        $username = Db::table('up_u_user')->where('user_phone',$phone)->find();
        if(empty($username)){
            return json(['status'=>3,'msg'=>'没有此手机号码用户']);
        }
        //=============================================
        $res = Db::table('up_phonecode')->where('phone',$phone)->find();
        //=============================================================
        if($code != $res['code']){
            return json(['status'=>21,'msg'=>'验证码错误']);
        }
        $now = time();
        if($now - $res['time2'] >= 301){
            return json(['status'=>22,'msg'=>'验证码超时']);
        }
        if($res['status']==1){
            return json(['status'=>23,'msg'=>'验证码已使用']);
        }
        $token = md5(date('YmdHis').rand(100000,999999));
        //,'user_token'=>$token
        $openid = $this->getOpenid();
        $res = Db::table('up_u_user')->where('user_phone',$phone)
            ->update(['user_password'=>md5($new),'user_token'=>$token,'user_openid'=>$openid]);
        Db::table('up_phonecode')->where('phone',$phone)->update(['status'=>1]);
        if($res){
            $user = Db::table('up_u_user')->where('user_phone',$phone)->find();
            return json(['status'=>200,'msg'=>$user]);
        }else{
            return json(['status'=>3,'msg'=>'密码重置失败']);
        }
    }

    /**
     * 更换手机号——1：给旧手机发送验证码
     * 参数： phone，code
     * @return string
     */
    public function changephone_old_yzm()
    {
        $phone = $_POST['phone'];
        $code = $_POST['code'];
        $username = Db::table('up_u_user')->where('user_phone',$phone)->find();
        if(empty($username)){
            return json(['status'=>3,'msg'=>'没有此手机号码用户']);
        }
        //=============================================
        $res = Db::table('up_phonecode')->where('phone',$phone)->find();
        //=============================================================
        if($code != $res['code']){
            return json(['status'=>21,'msg'=>'验证码错误']);
        }
        $now = time();
        if($now - $res['time2'] >= 301){
            return json(['status'=>22,'msg'=>'验证码超时']);
        }
        if($res['status']==1){
            return json(['status'=>23,'msg'=>'验证码已使用']);
        }
        return json(['status'=>200,'msg'=>'ok']);
    }
    /**
     * 更换手机号——2：提交新手机
     * 参数：phone，code
     * @return string
     */
    public function changephone_new_yzm()
    {
        $phone = trim($_POST['phone'],' ');
        $uid = $_POST['user_id'];
        $code = trim($_POST['code'],' ');
        if($phone == "" || $code == ""){
            return json(['status'=>0,'msg'=>'手机号和验证码不能为空']);
        }
        $username = Db::table('up_u_user')->where('user_phone',$phone)->find();
        if(!empty($username)){
            return json(['status'=>0,'msg'=>'已经有此手机号码用户']);
        }
        //=============================================================
        $res = Db::table('up_phonecode')->where('phone',$phone)->find();
        //=============================================================
        if(empty($res)){
            return json(['status'=>0,'msg'=>'不是同一个手机号']);
        }
        if($code != $res['code']){
            return json(['status'=>21,'msg'=>'验证码错误']);
        }
        $now = time();
        if($now - $res['time2'] >= 301){
            return json(['status'=>22,'msg'=>'验证码超时']);
        }
        if($res['status']==1){
            return json(['status'=>23,'msg'=>'验证码已使用']);
        }
        $res2 = Db::table('up_u_user')->where('user_id',$uid)->update(['user_phone'=>$phone]);
        if($res2){
            $new = Db::table('up_u_user')->where('user_id',$uid)->find();
            return json(['status'=>200,'msg'=>$new]);
        }else{
            return json(['status'=>0,'msg'=>'更新失败']);
        }

    }
    
}